
import React from "react";
import {DatePicker,Form, Input} from "antd";
import moment from "moment";

/**
 * 时间选择
 */
class FormTimePicker extends React.Component{
    constructor(props) {
        super(props);
        this.state = {}
    }

    handleOnChange = (date,dateString) => {
        let str = moment(date).format(this.props.format ? this.props.format : "YYYY-MM-DD HH:MM:ss")
        let obj = {};
        obj[this.props.field ? this.props.field : "default_time_picker"] = str;
        this.props.form.setFieldsValue(obj);
    };

    componentDidMount() {
        //设置初始值
        if (this.props.initialValue){
            let obj = {};
            obj[this.props.field ? this.props.field : "default_time_picker"] = this.props.initialValue;
            this.props.form.setFieldsValue(obj);
        }
    }

    render() {
        const {getFieldDecorator} =  this.props.form;
        /**
         * options 传一个对象 {key:value}
         */
        const {label,field,initialValue,rules,disabled,placeholder} = this.props;
        // <Input type={"hidden"}> 是没有办法的办法了...
        return (
            <Form.Item label={label ? label : "时间选择"}>
                {/*{getFieldDecorator(field ? field : 'default_date_picker', {initialValue: initialValue ? initialValue : null,rules: rules ? rules : []})(*/}
                {getFieldDecorator(field ? field : 'default_time_picker', {initialValue: "",rules: rules ? rules : []})(
                    <Input type={"hidden"}>
                    </Input>
                )}
                <DatePicker
                    style={{width:438}}
                    disabled={disabled ? disabled : false}
                    placeholder={placeholder ? placeholder : "请选择时间..."}
                    defaultValue={initialValue ? moment(initialValue) : null}
                    showTime
                    onChange={this.handleOnChange}
                />

            </Form.Item>
        );
    }
}

export default FormTimePicker;
